<?php

function encryptpwd($password, $salt) {
    return hash('sha256', md5($password) . $salt);
}

function getsalt($num = 6) {
    $str = 'abcdefghijklmnopqrstuvwxyz0123456789'; //源字符串
    $salt = '';
    for ($i = 0; $i < $num; $i++) {
        $char = rand(0, 35);
        $salt.=$str[$char];
    }
    return $salt;
}

function gettoken($username, $deviceid) {
    return hash('sha256', md5($username) . $deviceid);
}

/**
 * 邮件发送函数
 */
function sendMail($to, $title, $content) {

    Vendor('PHPMailer.PHPMailerAutoload');
    $mail = new PHPMailer(); //实例化
    $mail->IsSMTP(); // 启用SMTP
    $mail->Host = C('MAIL_HOST'); //smtp服务器的名称（这里以QQ邮箱为例）
    $mail->SMTPAuth = C('MAIL_SMTPAUTH'); //启用smtp认证
    $mail->Username = C('MAIL_USERNAME'); //你的邮箱名
    $mail->Password = C('MAIL_PASSWORD'); //邮箱密码
    $mail->From = C('MAIL_FROM'); //发件人地址（也就是你的邮箱地址）
    $mail->FromName = C('MAIL_FROMNAME'); //发件人姓名
    $mail->AddAddress($to, "尊敬的客户");
    $mail->WordWrap = 50; //设置每行字符长度
    $mail->IsHTML(C('MAIL_ISHTML')); // 是否HTML格式邮件
    $mail->CharSet = C('MAIL_CHARSET'); //设置邮件编码
    $mail->Subject = $title; //邮件主题
    $mail->Body = $content; //邮件内容
    $mail->AltBody = "这是一个纯文本的身体在非营利的HTML电子邮件客户端"; //邮件正文不支持HTML的备用显示
    return($mail->Send());
}

function timetodate($datetime) {
    $iTime = (int)strtotime($datetime);
    $dtime = time() - $iTime;
    switch ($dtime) {
        case $dtime<=0:
             $date = '0秒之前';
            break;
        case $dtime < 60:
            $date = $dtime . '秒之前';
            break;
        case $dtime < 3600:
            $date = ceil($dtime / 60) . '分钟之前';
            break;
        case $dtime < 86400 && (date('d') == date('d', $iTime)):
            $date = date('H:i');
            break;
        default:
            $date = date('Y-m-d H:i:s', $iTime);
            break;
    }
    return $date;
}
